package createtask;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import page.PageState;
import usr.Usr;
import weibo4j.WeiboException;
import channel.Channel;
import channel.ChannelMail;
import channel.ChannelWeibo;
import channel.ChannelYmail;

public class createtaskThat extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	{	//create a THAT channel and save in Session
		try {
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			String sid = req.getParameter("thatid");

			if(sid == null)
			{
				req.setAttribute("error_cause", PageState.UNKNOWED_ERROR);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			int id = Integer.parseInt(sid);
			
			if(id == Channel.WEIBO)	//Weibo
			{
				String weiboUsr = req.getParameter("weiboUsr");
				String pwd = req.getParameter("password");
				String status = req.getParameter("status");
				
				
				try {
					ChannelWeibo thatWeibo = new ChannelWeibo(weiboUsr, pwd, status);
					sess.setAttribute("that", thatWeibo);
				} catch (WeiboException e) {
					e.printStackTrace();
				}
				
				sess.setAttribute("thatType", id);
				resp.sendRedirect("newtask.jsp");
			}
			
			if(id == Channel.EMAIL)	//email
			{
				String to = req.getParameter("to");
				String subject = req.getParameter("subject");
				String body = req.getParameter("body");
				
				ChannelMail thatMail = new ChannelMail();
				thatMail.setTo(to);
				thatMail.setSubject(subject);
				thatMail.setBody(body);
				
				sess.setAttribute("that", thatMail);
				sess.setAttribute("thatType", id);
				resp.sendRedirect("newtask.jsp");
			}
			
			else if(id == Channel.YMAIL)	//ymail
			{
				String user = req.getParameter("user");
				String mailType = req.getParameter("mailType");
				String username = user + "@" + mailType;
				String pwd = req.getParameter("pwd");
				String to = req.getParameter("to");
				String subject = req.getParameter("subject");
				String body = req.getParameter("body");
				
				ChannelYmail thatYmail = new ChannelYmail();
				thatYmail.setYmail(username);
				thatYmail.setPassword(pwd);
				thatYmail.setTo(to);
				thatYmail.setSubject(subject);
				thatYmail.setBody(body);
				
				sess.setAttribute("that", thatYmail);
				sess.setAttribute("thatType", id);
				resp.sendRedirect("newtask.jsp"); 
			}
		}
		catch (IOException | ServletException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	{	//set attribute in order to allow "that.jsp" show much content
		try {
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			int id = Integer.parseInt(req.getParameter("thatid"));
			req.setAttribute("thatid", id);
			String steveid = req.getParameter("teveid");
			if(steveid != null)
			{
				int teveid = Integer.parseInt(steveid);
				req.setAttribute("teveid", teveid);
			}
			
			req.getRequestDispatcher("that.jsp").forward(req, resp);
		}
		
		catch (IOException | ServletException e) {
			try {
				resp.sendRedirect("u.html");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
